<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
$user_id = check_cookie();
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
$valide = (!empty($_REQUEST['valide'])) ? $_REQUEST['valide'] : "";
$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
if($id){
	$communication = new communication($id);
	if($action == 'del'){
		if(check_cookie() != $communication->membre_id) return 0;
		echo $communication->delete();
	}
}
elseif($user_id){
	if($action == 'list'){
		$membre = new membre($user_id);
		$where = "";
		foreach($membre->equipes as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";
		foreach($membre->entraineurs as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";		
		foreach($membre->responsables as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";
		$where = ($where) ? substr($where,0,-3) : $where;
		if($where){
			$q = "select communications.id as id from communications left join membre_communications on communications.id = communication_id where ($where) order by date desc";
			$r = ExecRequete($q);
			if(mysql_num_rows($r)){
				echo "<h2>Vos communications</h2>";
				while($s = LigneSuivante($r)){
					$communication = new communication($s->id);
					$communication->print_this();
				}
			}	
		}
	}
	elseif($valide){
		$valides = explode("_",$valide);
		foreach($valides as $comm_id){
			if($comm_id != 'lu' && strlen($comm_id)){
				$q = "insert into membre_communications (membre_id, communication_id) values ('$user_id','$comm_id')";
				ExecRequete($q);
			}
		}
	}
	else{
		$membre = new membre($user_id);
		$where = "";
		foreach($membre->equipes as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";
		foreach($membre->entraineurs as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";		
		foreach($membre->responsables as $eq_id => $eq_nom) $where .= "equipe_id = '$eq_id' or ";
		$where = ($where) ? substr($where,0,-3) : $where;
		if($where){
			$q = "select communications.id as id from communications left join membre_communications on communications.id = communication_id where ($where) and membre_communications.id is null and communications.membre_id != '$user_id'";
			$r = ExecRequete($q);
			if(mysql_num_rows($r)){
				echo "<h2>Nouvelles communications</h2>";
				echo "<form name = 'form_communications' action = 'javascript:valide_communications()' method = 'post' >";
				while($s = LigneSuivante($r)){
					$communication = new communication($s->id);
					$communication->print_this(1);
				}
				echo "<p class ='center'><button type = 'submit' name = 'lu' value = 'lu'>J'ai lu !</button></p>\n";
				echo "</form>\n";
			}					
		}
	}
}

?>
